﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ZombZomb.GameObjects
{
    /// <summary>
    /// Calcul la taille et la position du cadre de lecture dans la table de sprites
    /// </summary>
    public class FrameAnimation
    {

        // The first frame of the Animation.  We will calculate other
        // frames on the fly based on this frame.
        private Rectangle rectInitialFrame;

        //Temps en seconde d'apparition de chaque frame
        float vitesseAnimation = 0.1f;

        //Compteur du temps d'apparition d'une frame
        float tempsAnimation = 0.0f;

        //Taille d'une frame
        int frameWidth = 32;

        public int FrameWidth
        {
            get { return frameWidth; }
            set { frameWidth = value; }
        }
        int frameHeight = 32;

        public int FrameHeight
        {
            get { return frameHeight; }
            set { frameHeight = value; }
        }

        //Nombre de frames de l'animation
        int frameCount = 1;
        //Frame courante de l'animation
        int frameCourante = 0;
        // Nombre de fois que l'animation a été jouée
        private int iPlayCount = 0;
        //Animation suivante (facultatif)
        private string sNextAnimation = null;


        bool animating = true;



        public FrameAnimation(
                int FrameOffsetX,
                int FrameOffsetY,
                int FrameWidth,
                int FrameHeight,
                int FrameCount)
        {
            rectInitialFrame = new Rectangle(FrameOffsetX, FrameOffsetY, FrameWidth, FrameHeight);
            frameWidth = FrameWidth;
            frameHeight = FrameHeight;
            frameCount = FrameCount;
            
        }

        public FrameAnimation(
                int FrameOffsetX,
                int FrameOffsetY,
                int FrameWidth,
                int FrameHeight,
                int FrameCount,
                float VitesseAnimation) : this(FrameOffsetX, FrameOffsetY, FrameWidth, FrameHeight, FrameCount)
        {
            vitesseAnimation = VitesseAnimation;

        }

        public FrameAnimation(
                int FrameOffsetX,
                int FrameOffsetY,
                int FrameWidth,
                int FrameHeight,
                int FrameCount,
                float VitesseAnimation,
                String nextAnimation)
            : this(FrameOffsetX, FrameOffsetY, FrameWidth, FrameHeight, FrameCount, VitesseAnimation)
            
        {
            sNextAnimation = nextAnimation;
        }

        /// <summary>
        /// The rectangle associated with the current
        /// animation frame.
        /// </summary>
        public Rectangle FrameRectangle
        {
            get
            {
                return new Rectangle(
                    rectInitialFrame.X + (rectInitialFrame.Width * frameCourante),
                    rectInitialFrame.Y, rectInitialFrame.Width, rectInitialFrame.Height);
            }
        }

        public int CurrentFrame
        {
            get { return frameCourante; }
            set { frameCourante = (int)MathHelper.Clamp(value, 0, frameCount); }
        }



        public float FrameLength
        {
            get { return vitesseAnimation; }
            set { vitesseAnimation = (float)Math.Max(vitesseAnimation, 0f); }
        }



        public bool IsAnimating
        {
            get { return animating; }
            set { animating = value; }
        }

        public int PlayCount
        {
            get { return iPlayCount; }
            set { iPlayCount = value; }
        }



        public string NextAnimation
        {

            get { return sNextAnimation; }

            set { sNextAnimation = value; }

        }

        public void Update(GameTime gametime)
        {
            this.animating = true;
            
            if (animating)
            {
                // Accumulate elapsed time...
                tempsAnimation += (float)gametime.ElapsedGameTime.TotalSeconds;
                // Until it passes our frame length
                if (tempsAnimation > vitesseAnimation)
                {
                    // Increment the current frame, wrapping back to 0 at frameCount
                    frameCourante = (frameCourante + 1) % frameCount;
                    // Reset the elapsed frame time.
                    tempsAnimation = 0.0f;
                    //Check le nombre de fois que l'anim a été jouée
                    if (frameCourante == 0)
                    {
                        iPlayCount = (int)MathHelper.Min(iPlayCount + 1, int.MaxValue);
                    }
                }

                
            }
        }

//On ne met pas de méthode Draw(), celle ci se trouve dans la classe GameAnimatedSprite




    }
}
